package com.zhengke.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.zhengke.pojo.IndexPic;
import com.zhengke.pojo.Order;
import com.zhengke.pojo.SysDictionaryItem;
import com.zhengke.pojo.archie.CurrentUser;
import com.zhengke.pojo.archie.TvUser;
import com.zhengke.service.IndexService;
import com.zhengke.service.archie.IOrderDetailService;
@Controller
public class IndexController {
	
	public static final String SESSION_USER_KEY = "userinfo";
	
	@Autowired
	IndexService is;
	
	@Autowired
	IOrderDetailService orderService;
	/**
	 * 	去主界面
	 * @param paramHttpServletResponse
	 * @return
	 * 根据用户输入的id号进行判断是否有并将查到的内容放置到session域中去
	 */
	@RequestMapping("musk.do")
	public String goIndex(String tv_user_id,HttpServletRequest request,HttpServletResponse response,ModelMap model) {
		
		HttpSession session = request.getSession();
		//判断是否是空为空则返回到登录界面
		if(tv_user_id == null && "".equals(tv_user_id)) {
			//根据用户查询信息
			TvUser tvUser = orderService.querryTvUserByID(tv_user_id);
			//将用户对应的服务点的信息装到一个对象中并放到域中去
			session.setAttribute(SESSION_USER_KEY,tvUser);
			return "";
			//否则的话登录成功并将用户的个人信息放进域中去
		}else {
			//根据用户查询信息
			TvUser tvUser = orderService.querryTvUserByID(tv_user_id);
			CurrentUser cu = new CurrentUser();
			cu.setTvnNo(tvUser.getUser_id());//
			cu.setStationId(tvUser.getService_point_id());
			cu.setCityId(tvUser.getCity_id());
			cu.setUserName(tvUser.getUser_name());
			cu.setPhone(tvUser.getUser_phone());
			cu.setProvinceId(tvUser.getProvince_id());
			cu.setCountyId(tvUser.getCity_id());
			//cu.setStationId("");//服务点的信息暂且没有查询
			cu.setAddressDetail(tvUser.getAddress_detail());
			cu.setDeliveryWay(tvUser.getDelivery_way());
			cu.setReceiptTime(tvUser.getReceipt_time());
			cu.setUserBalance(new Double(tvUser.getUser_balance()));
			cu.setUserIntegral(new Double(tvUser.getUser_balance()));
			cu.setUserCardNo(tvUser.getUser_card_number());
			//将用户对应的服务点的信息装到一个对象中并放到域中去
			session.setAttribute(SESSION_USER_KEY,cu);
			model.put("rem",Math.random());
			session.setAttribute("tvnNo",tv_user_id);
			return "index";
		}
	}
	@RequestMapping("queryPics.do")
	 public void queryPics(HttpServletResponse response) throws IOException {
			List<IndexPic> listIp = is.selectPic();
			List<List<Map<String,String>>> rtnList = new ArrayList<List<Map<String,String>>>();
			List<Map<String,String>> otherList = new ArrayList<Map<String,String>>();
			for(IndexPic ip : listIp){
				if(ip.getId()==1){
					List<Map<String,String>> innerList = new ArrayList<Map<String,String>>();
					Map<String,String> innerMap = new HashMap<String,String>();
					if(ip.getPicPath()!=null){
						File f = new File(ip.getPicPath());
						if(!f.exists()){
							innerMap.put("pic_src", ip.getDefaultPic());
							innerMap.put("flag", "0");
						}else{
							innerMap.put("pic_src", ip.getPicPath());
							innerMap.put("flag", "1");
						}
					}else{
						innerMap.put("flag", "0");
						innerMap.put("pic_src", ip.getDefaultPic());
					}
					innerMap.put("url", "toDiscountList.do");
					innerList.add(innerMap);
					rtnList.add(innerList);
				}else{
					if(ip.getId()==2){
						Map<String,String> innerMap = new HashMap<String,String>();
						if(ip.getPicPath()!=null){
							File f = new File(ip.getPicPath());
							if(!f.exists()){
								innerMap.put("pic_src", ip.getDefaultPic());
								innerMap.put("flag", "0");
							}else{
								innerMap.put("pic_src", ip.getPicPath());
								innerMap.put("flag", "1");
							}
						}else{
							innerMap.put("flag", "0");
							innerMap.put("pic_src", ip.getDefaultPic());
						}
						innerMap.put("url", "toSalesList.do");
						otherList.add(innerMap);
					}
					if(ip.getId()==3){
						Map<String,String> innerMap = new HashMap<String,String>();
						if(ip.getPicPath()!=null){
							File f = new File(ip.getPicPath());
							if(!f.exists()){
								innerMap.put("pic_src", ip.getDefaultPic());
								innerMap.put("flag", "0");
							}else{
								innerMap.put("pic_src", ip.getPicPath());
								innerMap.put("flag", "1");
							}
						}else{
							innerMap.put("flag", "0");
							innerMap.put("pic_src", ip.getDefaultPic());
						}
						innerMap.put("url", "toBrandList.do");
						otherList.add(innerMap);
					}
					if(ip.getId()==4){
						Map<String,String> innerMap = new HashMap<String,String>();
						if(ip.getPicPath()!=null){
							File f = new File(ip.getPicPath());
							if(!f.exists()){
								innerMap.put("pic_src", ip.getDefaultPic());
								innerMap.put("flag", "0");
							}else{
								innerMap.put("pic_src", ip.getPicPath());
								innerMap.put("flag", "1");
							}
						}else{
							innerMap.put("flag", "0");
							innerMap.put("pic_src", ip.getDefaultPic());
						}
						innerMap.put("url", "toTypeList.do");
						otherList.add(innerMap);
					}
					if(ip.getId()==5){
						Map<String,String> innerMap = new HashMap<String,String>();
						if(ip.getPicPath()!=null){
							File f = new File(ip.getPicPath());
							if(!f.exists()){
								innerMap.put("pic_src", ip.getDefaultPic());
								innerMap.put("flag", "0");
							}else{
								innerMap.put("pic_src", ip.getPicPath());
								innerMap.put("flag", "1");
							}
						}else{
							innerMap.put("flag", "0");
							innerMap.put("pic_src", ip.getDefaultPic());
						}
						innerMap.put("url", "toExperieList.do");
						otherList.add(innerMap);
					}
					if(ip.getId()==6){
						Map<String,String> innerMap = new HashMap<String,String>();
						if(ip.getPicPath()!=null){
							File f = new File(ip.getPicPath());
							if(!f.exists()){
								innerMap.put("pic_src", ip.getDefaultPic());
								innerMap.put("flag", "0");
							}else{
								innerMap.put("pic_src", ip.getPicPath());
								innerMap.put("flag", "1");
							}
						}else{
							innerMap.put("flag", "0");
							innerMap.put("pic_src", ip.getDefaultPic());
						}
						innerMap.put("url", "toIntegralList.do");
						otherList.add(innerMap);
					}
					if(ip.getId()==7){
						Map<String,String> innerMap = new HashMap<String,String>();
						if(ip.getPicPath()!=null){
							File f = new File(ip.getPicPath());
							if(!f.exists()){
								innerMap.put("pic_src", ip.getDefaultPic());
								innerMap.put("flag", "0");
							}else{
								innerMap.put("pic_src", ip.getPicPath());
								innerMap.put("flag", "1");
							}
						}else{
							innerMap.put("flag", "0");
							innerMap.put("pic_src", ip.getDefaultPic());
						}
						innerMap.put("url", "toAccountCenter.do");
						otherList.add(innerMap);
					}
				}
			}
			rtnList.add(otherList);	
			
			List<Map<String,String>> thirdList = new ArrayList<Map<String,String>>();
			Map<String,String> thirdMap = new HashMap<String,String>();
			thirdMap.put("pic_src", "/images/index/pic8.png");
			thirdMap.put("flag", "0");
			thirdMap.put("url", "toHelpList.do");//当前的请求是为了跳转到一个界面的
			thirdList.add(thirdMap);
			rtnList.add(thirdList);	
			
			response.setCharacterEncoding("utf-8");
			response.setHeader("Pragma","No-cache"); 
			response.setHeader("Cache-Control","no-cache"); 
			response.setDateHeader("Expires", 0);  
			response.getWriter().print("var dataList="+JSONArray.toJSON(rtnList));	
	 }
	@RequestMapping("selectNews.do")
	public void selectNews(HttpServletResponse response) throws IOException {
		
		String userId = "24885492";
	
			//查询有没有已配货和已发货的订单
			List<Order> orderList = is.selectOrderByUserId(userId);
			//查询新闻
			List<SysDictionaryItem> itemList = is.selectSysDictionaryItemByDicId("0007");
			//查询滚动时间
			List<SysDictionaryItem> timeList = is.selectSysDictionaryItemByDicId("0008");
			List<String> list = new ArrayList<String>();
			if(timeList.size()>0){
				list.add(timeList.get(0).getParamValue());
			}
			if(orderList.size()>0){
				list.add("温馨提示：您的订单正在配送中......");
			}
			for(SysDictionaryItem item:itemList){
				list.add(item.getParamValue());
			}
			response.setCharacterEncoding("utf-8");
			response.setHeader("Pragma","No-cache"); 
			response.setHeader("Cache-Control","no-cache"); 
			response.setDateHeader("Expires", 0);  
			response.getWriter().print("var marqueeContent="+JSONArray.toJSON(list));
		
	}
}
